/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chess;

/**
 *
 * @author LostOdyssey
 */
public class ActionChess {
    private Chess[][] myChess = new Chess[10][10];
    private Chess[][] saveChess = new Chess[10][10];
    
    public static void main(String args[]){
        ActionChess actionChess = new ActionChess();
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                System.out.print(actionChess.getValueOfChess(i, j)+"  ");
            }
                System.out.println();
        }
    }
    public ActionChess(){
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                myChess[i][j] = new Chess(i,j);
                if(i==4&&j==4){
                    myChess[i][j].setValue(0);                    
                }                
                if(i==4&&j==5){
                    myChess[i][j].setValue(1);
                }                
                if(i==5&&j==4){
                    myChess[i][j].setValue(1);                    
                }
                if(i==5&&j==5){
                    myChess[i][j].setValue(0);                    
                }
            }
        }
    }
    
    public void createChess(){
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                myChess[i][j] = new Chess(i,j);
                if((i==4&&j==4)|(i==5&&j==5)){
                    myChess[i][j].setValue(0);                    
                }
                if((i==5&&j==4)|(i==4&&j==5)){
                    myChess[i][j].setValue(1);
                }
            }
        }
    }
    
    public void saveChess() {         
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                int r = myChess[i][j].getRow();
                int c = myChess[i][j].getColumn();
                int v = myChess[i][j].getValue();
                saveChess[i][j].setChess(r, c, v);
            }
        }
    }
    
    public void resetChess() {
        this.createChess();
        this.saveChess();
    }
    
    public int getValueOfChess(int row, int column) {        
        int value;        
        value = myChess[row][column].getValue();            
        return value;
    }
    
    public void getValueOfSaveChess() {
        int[][] value = new int[10][10];
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                value[i][i] = saveChess[i][j].getValue();
            }
        }
    }
    
    public void setChessBoard(int row, int column, int value){          
        myChess[row][column].setChess(row,column,value);                          
    }
            
    public Chess checkLeft(int row, int column, int value) {        
        if(column>=2 && (myChess[row][column-1].getValue()!=value && myChess[row][column-1].getValue()!= -1)) {
            for(int i=column-2; i>=0; i--){
                if(myChess[row][i].getValue()==value) {
                    return myChess[row][i];                     
                }
            }            
        }    
        Chess checkLeft = new Chess();
        return checkLeft;
    }
    public Chess checkRight(int row, int column, int value) {        
        if(column<=7 && (myChess[row][column+1].getValue()!=value && myChess[row][column+1].getValue()!= -1)) {
            for(int i=column+2; i<=9; i++){
                if(myChess[row][i].getValue()==value){
                    return myChess[row][i];
                }
            }            
        }         
        Chess checkRight = new Chess();    
        return checkRight;
    }
    public Chess checkDown(int row, int column, int value){        
        if(row<=7 && (myChess[row+1][column].getValue()!=value && myChess[row+1][column].getValue() != -1)){
            for(int i=row+2; i<=9; i++){
                if(myChess[i][column].getValue()==value){
                    return myChess[i][column];
                }
            }
        }
        Chess checkDown = new Chess();
        return checkDown;
    }
    public Chess checkUp(int row, int column, int value){        
        if(row>=2 && (myChess[row-1][column].getValue()!=value && myChess[row-1][column].getValue()!= -1)){
            for(int i=row-2; i>=0; i--){
                if(myChess[i][column].getValue()==value){
                    return myChess[i][column];
                }
            }
        }
        Chess checkUp = new Chess();
        return checkUp;
    }
    public Chess checkDiagonalLeftUp(int row, int column, int value){        
        if(row>=2 && column>=2 && (myChess[row-1][column-1].getValue()!= value && myChess[row-1][column-1].getValue()!= -1)){
            int i = row-2; int j = column-2;
            while(i>=0 && j>=0){
                if(myChess[i][j].getValue()== value){
                    return myChess[i][j];
                }
                i--; j--;
            }
        }      
        Chess checkDiagonalLeftUp = new Chess();
        return checkDiagonalLeftUp;
    }
    public Chess checkDiagonalRightUp(int row, int column, int value){        
        if(row>=2 && column<=7 && (myChess[row-1][column+1].getValue()!= value && myChess[row-1][column+1].getValue()!= -1)){
            int i = row-2; int j = column+2;
            while(i>=0 && j<=9){
                if(myChess[i][j].getValue()== value){
                    return myChess[i][j];
                }
                i--; j++;
            }
        }
        Chess check = new Chess();
        return check;
    }
    public Chess checkDiagonalLeftDown(int row, int column, int value){        
        if(row<=7 && column>=2 && (myChess[row+1][column-1].getValue()!= value && myChess[row+1][column-1].getValue()!= -1)){
            int i = row+2; int j = column-2;
            while(i<=9 && j>=0){
               if(myChess[i][j].getValue()== value){
                   return myChess[i][j];
               } 
               i++; j--;
            }
        }
        Chess check = new Chess();
        return check;
    }
    public Chess checkDiagonalRightDown(int row, int column, int value){        
        if(row<=7 && column<=7 && (myChess[row+1][column+1].getValue()!= value && myChess[row+1][column+1].getValue()!= -1)){
            int i = row+2; int j = column+2;
            while(i<=9 && j<=9){
                if(myChess[i][j].getValue()== value){
                    return myChess[i][j];                    
                }
                i++; j++;
            }
        }
        Chess check = new Chess();
        return check;
    }
    
    public void turnValue(int row, int column){
        if(myChess[row][column].getValue()== 0) {
            myChess[row][column].setValue(1);
        }
        else {
            myChess[row][column].setValue(0);
        }
    }
    
    public boolean checkChess(int row, int column, int value) {       
        int checkLeft = this.checkLeft(row, column, value).getValue();
        int checkRight = this.checkRight(row, column, value).getValue();
        int checkUp = this.checkUp(row, column, value).getValue();
        int checkDown = this.checkDown(row, column, value).getValue();
        int checkDiagonalLeftUp = this.checkDiagonalLeftUp(row, column, value).getValue();
        int checkDiagonalRightUp = this.checkDiagonalRightUp(row, column, value).getValue();
        int checkDiagonalLeftDown = this.checkDiagonalLeftDown(row, column, value).getValue();
        int checkDiagonalRightDown = this.checkDiagonalRightDown(row, column, value).getValue();
        int checkThis = myChess[row][column].getValue();
        
        if(checkLeft==value | checkRight==value | checkUp==value | checkDown==value |checkDiagonalLeftUp == value
            | checkDiagonalRightUp == value | checkDiagonalLeftDown == value | checkDiagonalRightDown == value){
            return true;
        }
        return false;
    }
    
    public void changeChessBoard(int row, int column, int value) {
        int checkLeft = this.checkLeft(row, column, value).getValue();
        int checkRight = this.checkRight(row, column, value).getValue();
        int checkUp = this.checkUp(row, column, value).getValue();
        int checkDown = this.checkDown(row, column, value).getValue();
        int checkDiagonalLeftUp = this.checkDiagonalLeftUp(row, column, value).getValue();
        int checkDiagonalRightUp = this.checkDiagonalRightUp(row, column, value).getValue();
        int checkDiagonalLeftDown = this.checkDiagonalLeftDown(row, column, value).getValue();
        int checkDiagonalRightDown = this.checkDiagonalRightDown(row, column, value).getValue();
        
        if(checkLeft == value) {            
            int c = this.checkLeft(row, column, value).getColumn();
            for(int i=column-1; i>c; i--) {
                this.turnValue(row, i);
            }            
        }
        if(checkRight ==  value) {
            int c = this.checkRight(row, column, value).getColumn();
            for(int i=column+1; i<c; i++) {
                this.turnValue(row, i);
            }            
        }
        if(checkUp == value) {
            int r = this.checkUp(row, column, value).getRow(); int i=row-1;
            while(i>r){
                this.turnValue(i, column); i--;                
            }
        }
        if(checkDown == value){
            int r = this.checkDown(row, column, value).getRow(); int i=row+1;
            while(i<r){
                this.turnValue(i, column); i++;
            }
        }
        if(checkDiagonalLeftUp == value){
            Chess checkLu = this.checkDiagonalLeftUp(row, column, value);
            int r = checkLu.getRow(); int c = checkLu.getColumn();
            int i=row-1; int j=column-1;
            while(i>r && j>c){
                this.turnValue(i, j);
                i--; j--;
            }
        }
        if(checkDiagonalRightUp == value){
            Chess checkRu = this.checkDiagonalRightUp(row, column, value);
            int r = checkRu.getRow(); int c = checkRu.getColumn();
            int i=row-1; int j=column+1;
            while(i>r && j<c){
                this.turnValue(i, j);
                i--; j++;
            }
        }
        if(checkDiagonalLeftDown == value){
            Chess checkLd = this.checkDiagonalLeftDown(row, column, value);
            int r = checkLd.getRow(); int c = checkLd.getColumn();
            int i = row+1; int j =column-1;
            while(i<r && j>c){
                this.turnValue(i, j);
                i++; j--;
            }
        }
        if(checkDiagonalRightDown == value){  
            Chess checkRd = this.checkDiagonalRightDown(row, column, value);            
            int r = checkRd.getRow(); int c = checkRd.getColumn();
            int i=row+1; int j=column+1;
            while(i<r && j<c){
                this.turnValue(i, j);
                i++; j++;
            }
        } 
    }
    
    public boolean checkWin() {        
        int[][] value = new int[10][10];
        int count = 1;
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                if(myChess[i][j].getValue()==0) {
                    count++;
                }
            }
        }
        if(count > 50){
            return true;
        }
        return false;
    }
    
    public void inbancon(int r, int c){
        for(int i=0; i<=9; i++){
            for(int j=0; j<=9; j++){
                System.out.print(myChess[i][j].getValue()+"  ");                
            }
            System.out.println();
        }
    }
    
}
